#include <vector>
#include "encoder.hpp"

Encoder::Encoder(){
    header.push_back(0xAA);
    footer.push_back(0x55);
    data_length = 0;
}

void Encoder::setDataLength(int length){
    data_length = length;
}

std::vector<char> Encoder::encode(){
    std::vector<char> ret;

    union DataLenght{
        unsigned int length;
        char buf[sizeof(unsigned int)];
    };

    union DataLenght data_length;

    data_length.length = this->data_length;

    for(unsigned int i = 0; i < header.size(); i++){
        ret.push_back(header.at(i));
    }

    for(unsigned int i = 0; i < sizeof(DataLenght); i++){
        ret.push_back(data_length.buf[i]);
    }

    for(unsigned int i = 0; i < data_buff.size(); i++){
        ret.push_back(data_buff.at(i));
    }

    for(unsigned int i = 0; i < footer.size(); i++){
        ret.push_back(footer.at(i));
    }

    return ret;
}


/*-------------------------------------------------*/

PosEncoder::PosEncoder():Encoder(){
    data_length = sizeof(int) * 2;
}

std::vector<char> PosEncoder::encode(int x, int y){
    std::vector<char> ret;

    union DataUnion{
        struct{
            int x;
            int y;
        }data;
        char buf[sizeof(int) * 2];
    };

    union DataUnion data;

    data.data.x = x;
    data.data.y = y;

    data_buff.clear();

    for(unsigned int i = 0; i < sizeof(int) * 2; i++){
        data_buff.push_back(data.buf[i]);
    }

    ret = Encoder::encode();

    return ret;
}
